package com.livepass.android.girlspicks.utils;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.livepass.android.girlspicks.models.Article;
import com.livepass.android.girlspicks.models.Comment;
import com.livepass.android.girlspicks.models.Notification;

public class JSONParser {

	public JSONParser() {
		super();
	}

	public ArrayList<Article> parserArticle(JSONObject object) {
		ArrayList<Article> arrayList = new ArrayList<Article>();
		try {
			JSONArray dataJsonArray = object.getJSONArray("data");

			for(int i=0; i<dataJsonArray.length(); i++) {
				JSONObject jsonObj = dataJsonArray.getJSONObject(i);
				Article article = new Article();
				article.setArticleId(jsonObj.getString("article_id"));
				article.setCreatedAt(jsonObj.getString("created_at"));
				article.setUpdatedAt(jsonObj.getString("updated_at"));
				article.setSite(jsonObj.getString("site"));
				article.setTitle(jsonObj.getString("title"));
				article.setOverview(jsonObj.getString("overview"));
				article.setUrl(jsonObj.getString("url"));
				article.setCategoryId(jsonObj.getString("category_id"));
				article.setImage(jsonObj.getString("image"));
				article.setCmtCount(jsonObj.getString("cmt_count"));
				
				arrayList.add(article);
			}
		} catch (JSONException e) {
			Log.d("JSONParser => parserArticle", e.getMessage());
		}
		return arrayList;
	}
	
	public Article parserArticleDetail(JSONObject object) {
		Article article = new Article();
		try {
			JSONObject jsonObj = object.getJSONArray("data").getJSONObject(0);
			article.setArticleId(jsonObj.getString("article_id"));
			article.setCreatedAt(jsonObj.getString("created_at"));
			article.setUpdatedAt(jsonObj.getString("updated_at"));
			article.setSite(jsonObj.getString("site"));
			article.setTitle(jsonObj.getString("title"));
			article.setOverview(jsonObj.getString("overview"));
			article.setUrl(jsonObj.getString("url"));
			article.setCategoryId(jsonObj.getString("category_id"));
			article.setImage(jsonObj.getString("image"));
			article.setCmtCount(jsonObj.getString("cmt_count"));
		} catch (JSONException e) {
			Log.d("JSONParser => parserArticle", e.getMessage());
		}
		return article;
	}
	
	public ArrayList<Comment> parserComment(JSONObject object) {
		ArrayList<Comment> arrayList = new ArrayList<Comment>();
		try {
			if (object.getString("data").length() > 2) {
				JSONArray ja = object.getJSONArray("data");
				for(int i=0; i<ja.length(); i++) {
					JSONObject jsonObj = ja.getJSONObject(i);
					Comment comment = new Comment();
					comment.setArticleId(jsonObj.getString("article_id"));
					comment.setCommentId(jsonObj.getString("comment_id"));
					comment.setClientId(jsonObj.getString("client_id"));
					comment.setContent(jsonObj.getString("content"));
					comment.setCreatedAt(jsonObj.getString("created_at"));
					comment.setUpdatedAt(jsonObj.getString("updated_at"));
					comment.setIsDeleted(jsonObj.getString("is_deleted"));
					comment.setLikeCount(jsonObj.getInt("like_count"));
					comment.setDislikeCount(jsonObj.getInt("dislike_count"));
					comment.setTypeLike(jsonObj.getInt("like_type"));
					// create Reply list

					if (jsonObj.getString("reply_list").toString().length() > 2) {
						JSONArray jaReply = jsonObj.getJSONArray("reply_list");
						comment.setListReply(parserReply(jaReply));
						comment.setReplyCount(""+jaReply.length());
					} else {
						comment.setListReply(new ArrayList<Comment>());
						comment.setReplyCount("0");
					}
					arrayList.add(comment);
				}
			}
		} catch (JSONException e) {
			Log.d("JSONParser => parserComment", e.getMessage());
		}
		return arrayList;
	}
	
	public String parserPostComment(JSONObject object) {
		String res = null;
		try {
			if (object.getInt("status") == 0) {
				res = object.getString("data");
				//res = data.getJSONObject(0).getString("comment_id");
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	public ArrayList<Comment> parserReply(JSONArray object) {
		ArrayList<Comment> arrayList = new ArrayList<Comment>();
		try {
			for(int i=0; i<object.length(); i++) {
				JSONObject jsonObj = object.getJSONObject(i);
				Comment reply = new Comment();
				reply.setCommentId(jsonObj.getString("reply_id"));
				reply.setClientId(jsonObj.getString("client_id"));
				reply.setContent(jsonObj.getString("content"));
				reply.setCreatedAt(jsonObj.getString("created_at"));
				reply.setUpdatedAt(jsonObj.getString("updated_at"));
				reply.setIsDeleted(jsonObj.getString("is_deleted"));
				reply.setLikeCount(jsonObj.getInt("like_count"));
				reply.setDislikeCount(jsonObj.getInt("dislike_count"));
				reply.setTypeLike(jsonObj.getInt("like_type"));
				arrayList.add(reply);
			}
		} catch (JSONException e) {
			Log.d("JSONParser => parserReply", e.getMessage());
		}
		return arrayList;
	}
	
	/**
	 * 
	 * @param object
	 * @return user id
	 */
	public String parserUserId(JSONObject object) {
		String res = null;
		try {
			if (object != null) {
				JSONArray data = object.getJSONArray("data");
				res = data.getJSONObject(0).getString("id");
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return res;
		}
		return res;
	}
	
	public ArrayList<Notification> parserNotification(JSONObject object) {
		ArrayList<Notification> arrayList = new ArrayList<Notification>();
		try {
			JSONArray data = object.getJSONArray("data");
			for(int i=0; i<data.length(); i++) {
				JSONObject jsonObj = data.getJSONObject(i);
				Notification notif = new Notification();
				notif.setNotifId(jsonObj.getString("notif_id"));
				notif.setFromClient(jsonObj.getString("from_client"));
				notif.setToClient(jsonObj.getString("to_client"));
				notif.setArticleId(jsonObj.getString("article_id"));
				notif.setType(jsonObj.getInt("type"));
				notif.setIsRead(jsonObj.getString("is_read"));
				notif.setCreatedAt(jsonObj.getString("created_at"));
				//notif.setUpdatedAt(jsonObj.getString("updated_at"));
				
				arrayList.add(notif);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			Log.d("JSONParser => parserNotification", e.getMessage());
		}
		return arrayList;
	}
	
	public String parserSetReadNotification(JSONObject object) {
		String res = null;
		try {
			if (object != null) {
				JSONObject data = object.getJSONObject("data");
				res = data.	getString("status");
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return res;
		}
		return res;
	}
}